package com.dw.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.dw.entity.OrderInfo;
import com.dw.feign.ProductFeign;
import com.dw.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
@RefreshScope
public class OrderController {
    @Autowired
    RestTemplate restTemplate;

    @Autowired
    OrderService orderService;

    @Autowired
    ProductFeign productFeign;


    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name") String name) { //http://localhost:9091/order/hello?name=daiwei
        System.out.println("===订单模块:hello order==="+name);
        return "===订单模块:hello order==="+name;
    }

    @GetMapping("/makeOrder")
    public String makeOrder() throws InterruptedException {  //http://localhost:9091/order/makeOrder
        //String str = restTemplate.getForObject("http://localhost:9091/product/reduceProduct", String.class);
        //String str = restTemplate.getForObject("http://product-module/product/reduceProduct", String.class);
        //Thread.sleep(10000);
        orderService.makeOrder();
        return "===订单下单成功:makeOrder---------------";
    }


    @GetMapping("/makeOrder2")
    @SentinelResource(value = "flow",blockHandler = "flowBlockHandler")
    public String makeOrder2() throws InterruptedException {  //http://localhost:9091/order/makeOrder
        //String str = restTemplate.getForObject("http://localhost:9091/product/reduceProduct", String.class);
        //String str = restTemplate.getForObject("http://product-module/product/reduceProduct", String.class);
        //Thread.sleep(10000);
        orderService.makeOrder();
        return "===订单下单成功:makeOrder---------------";
    }
    public String flowBlockHandler(BlockException e){
        return "===流控成功===";
    }
}
